﻿Imports Mystery

Public Class ContentView
    Implements IContentShower, IContentModificationsSubriver

    Private _first_time As Boolean = True
    Private _properties As IEnumerable(Of ContentProperty)

    Private _content As IContent
    Private _controls As New List(Of IPropertyControl)
    Private _content_type As ContentType

    Public Function show(content As Mystery.IContent) As Boolean Implements IContentShower.show
        If content Is Nothing Then Return False

        If _content_type Is Nothing Then
            _content_type = content.GetType.getMysteryAttribute(Of ContentType)()
        End If

        'checking type
        If _content_type IsNot content.GetType.getMysteryAttribute(Of ContentType)() Then
            Return False
        End If

        _redering_content = True

        _content = content

        Me.Dispatcher.Invoke(Sub() panelActions.Children.Clear())
        If _content IsNot Nothing Then
            For Each ba As IContentActionButton In _content.GetType.getMysteryAttribute(Of ContentActionButtonAttr).getAll(_content)
                Dim local_copy As IContentActionButton = ba
                Me.Dispatcher.Invoke(Sub() panelActions.Children.Add(local_copy.createSmallButton()))
            Next
        End If


        If _first_time AndAlso _content IsNot Nothing Then
            Me.Dispatcher.Invoke(Sub() generateControl())
        End If

        For Each pc As IPropertyControl In _controls
            pc.content = content
        Next

        _redering_content = False

        Return True

    End Function

    Private Sub generateControl()
        Dim type As Type = _content.GetType()

        Dim property_controls As New HashSet(Of ContentProperyViewAttr)(getMystery.AssemblyRegister.getMysteryPropertyAttributes(Of ContentProperyViewAttr)(type, include_inherits:=True))
        _content_type = type.getMysteryAttribute(Of ContentType)()

        Dim index As Integer = 0
        Dim rd As RowDefinition

        For Each pc As ContentProperyViewAttr In property_controls

            rd = New RowDefinition
            stack.RowDefinitions.Add(rd)
            Dim c As UserControl = pc.createControl(_content)

            Grid.SetRow(c, index)
            Grid.SetColumn(c, 1)
            stack.Children.Add(c)

            If TypeOf (c) Is IPropertyControl Then
                Dim property_control As IPropertyControl = c
                'labeling
                Dim plabel As New Label
                plabel.Content = property_control.label
                Grid.SetRow(plabel, index)
                Grid.SetColumn(plabel, 0)
                stack.Children.Add(plabel)
                _controls.Add(c)
                rd.Height = property_control.wished_height
                AddHandler property_control.valueChanged, AddressOf EnableSave
            End If

            index += 1
        Next



        _first_time = False

    End Sub

    Private _redering_content As Boolean = False

    Private Sub EnableSave()
        If Not _redering_content Then
            Me.Dispatcher.Invoke(Sub()
                                     bSave.IsEnabled = True
                                     panelActions.Children.Clear()
                                 End Sub)
        End If

    End Sub


    Private Sub bSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles bSave.Click
        Dim contents As New HashSet(Of IContent)
        For Each pc As IPropertyControl In _controls
            contents.addRange(pc.setInContent())
        Next
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        cd.addContents(contents)

        'to refresh everything about this content
        Dim ir As InterfaceStatus = getGlobalObject(Of InterfaceStatus)()
        ir.showContent(_content, True)
        bSave.IsEnabled = False
    End Sub

    Public ReadOnly Property showen_content As Mystery.IContent Implements IContentShower.showen_content
        Get
            Return _content
        End Get
    End Property

    Public ReadOnly Property HasUnsavedMofications As Boolean
        Get
            Dim result As Boolean = False
            Me.Dispatcher.Invoke(Sub() result = bSave.IsEnabled)
            Return result
        End Get
    End Property

    Public Sub notificate() Implements Mystery.IContentModificationsSubriver.notificate
        If _content Is Nothing Then Exit Sub
        If HasUnsavedMofications Then Exit Sub

        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        _content = cd.getContent(Of IContent)(_content.guid)
        If _content Is Nothing Then
            'it has been deleted
            Me.Dispatcher.Invoke(Sub() panelActions.Children.Clear())
            For Each pc As IPropertyControl In _controls
                pc.content = Nothing
            Next
        Else
            Me.show(_content)
        End If

    End Sub

    Public ReadOnly Property subscribed As Mystery.IContent Implements Mystery.IContentModificationsSubriver.subscribed
        Get
            Return _content
        End Get
    End Property

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim cm As ContentModifications = getGlobalObject(Of ContentModifications)()
        cm.subscrive(Me)
    End Sub

    Public Sub DropContent(content As Mystery.IContent) Implements IContentShower.DropContent
        If _content IsNot Nothing AndAlso _content.guid = content.guid Then
            'oh boy
            Me.Dispatcher.Invoke(Sub()

                                     'it has been deleted
                                     panelActions.Children.Clear()
                                     For Each pc As IPropertyControl In _controls
                                         pc.content = Nothing
                                     Next
                                 End Sub)
        End If
    End Sub


End Class
